package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.FocusUser;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class FocusUserApiImpl implements FocusUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<FocusUser> getFocusUsers(Long userId, List<Long> ids) {
        Query query = Query.query(Criteria.where("followUserId").is(userId)
                .and("userId").in(ids));
        return mongoTemplate.find(query, FocusUser.class);
    }

    @Override
    public boolean exist(FocusUser focusUser) {
        Query query = Query.query(Criteria.where("userId").is(focusUser.getUserId())
                .and("followUserId").is(focusUser.getFollowUserId()));
        return mongoTemplate.exists(query, FocusUser.class);
    }

    @Override
    public String save(FocusUser focusUser) {
        mongoTemplate.save(focusUser);
        return focusUser.getId().toHexString();
    }

    @Override
    public void remove(FocusUser focusUser) {
        Query query = Query.query(Criteria.where("userId").is(focusUser.getUserId())
                .and("followUserId").is(focusUser.getFollowUserId()));
        mongoTemplate.remove(query, FocusUser.class);
    }
}
